import message from './message'

//封装后的系统api
class SystemAPI {
    constructor() {

    }

    // 读取文件
    readFile(file) {
        return new Promise((resolve, reject) => {
            let messageId=message.sendMessage({
                type: 'readFile',
                data: {file}
            });
            message.receiveMessage((msg)=>{
                if(msg.messageId==messageId){
                    resolve(msg.result)
                }
            })
        })
    }
    // 写入文件
    writeFile(file,content) {
        return new Promise((resolve, reject) => {
            let messageId=message.sendMessage({
                type: 'writeFile',
                data: {file,content}
            });
            message.receiveMessage((msg)=>{
                if(msg.messageId==messageId){
                    resolve(msg.result)
                }
            })
        })
    }

    // 检查文件,文件夹是否存在
    exists(file) {
        return new Promise((resolve, reject) => {
            let messageId=message.sendMessage({
                type: 'exists',
                data: {file}
            });
            message.receiveMessage((msg)=>{
                if(msg.messageId==messageId){
                    resolve(msg.result)
                }
            })
        })
    }

    //
    homeDir() {
        return new Promise((resolve, reject) => {
            let messageId=message.sendMessage({
                type: 'homeDir',
                data: {}
            });
            message.receiveMessage((msg)=>{
                if(msg.messageId==messageId){
                    resolve(msg.result)
                }
            })
        })
    }

    //设置数据缓存
    setStorage(key,value){
        message.getMessageResult({
            type: 'setStorage',
            data: {key,value}
        });
    }
    //获取数据缓存
    getStorage(key,callback){
        message.getMessageResult({
            type: 'getStorage',
            data: {key},
            callback:(result)=>{
                callback(result)
            }
        });
    }
    //删除数据
    removeStorage(key,callback){
        message.getMessageResult({
            type: 'removeStorage',
            data: {key},
            callback:(result)=>{
                callback(result)
            }
        });
    }
    //清除数据缓存
    clearStorage(callback){
        message.getMessageResult({
            type: 'clearStorage',
            data: {},
            callback:(result)=>{
                callback(result)
            }
        });
    }
    //从客户端会话选择文件
    chooseMessageFile(data){
        let result=message.getMessageResult({
            type: 'openDialog',
            data: data
        });
    }
    //设置系统剪贴板的内容
    setClipboardData(data){
        let result=message.getMessageResult({
            type: 'setClipboardData',
            data: data
        });
    }
    //获取系统剪贴板的内容
    getClipboardData(data){
        let result=message.getMessageResult({
            type: 'getClipboardData',
            data: data
        });
    }
    //打开外部文件
    openExternalFile(data){
        let result=message.getMessageResult({
            type: 'openExternalFile',
            data: data
        });
    }



}
export default new SystemAPI()